package com.example.demo.service.impl;

import com.example.demo.dao.BookDao;
import com.example.demo.entity.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Override
    public List<Book> listAll() {
        return (List<Book>) bookDao.findAll();
    }

    @Override
    public int save(Book book) {
        return bookDao.save(book) != null ? 1 : 0;
    }

    @Override
    public int delete(int bookId) {
        int i = 0;
        try {
            bookDao.deleteById(bookId);
            i = 1;
        } catch (Exception e) {
            e.printStackTrace();
            i = 0;
        }
        return i;
    }

    @Override
    public int update(Book book) {
        return bookDao.save(book) != null ? 1 : 0;
    }



}
